package com.ruoyi.partybuilding.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.partybuilding.common.DataUtil;
import com.ruoyi.partybuilding.common.EQueryController;
import com.ruoyi.partybuilding.common.PageSort;
import com.ruoyi.partybuilding.common.TableDataInfoRows;
import com.ruoyi.partybuilding.domain.NewsSite;
import com.ruoyi.partybuilding.domain.NewsSite;
import com.ruoyi.partybuilding.service.impl.OrgServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Api(tags = "资讯站点对象功能接口")
@RestController
@RequestMapping("/partybuilding/NewsSite")
public class NewSiteController extends EQueryController<NewsSite> {
    @Resource
    OrgServiceImpl orgService;
    /**
     * 查询新闻栏目列表
     */
    @Anonymous
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('partybuilding:NewsSite:list')")
    @GetMapping("/list")
    @DataScope
    public TableDataInfoRows<NewsSite> list(NewsSite entity, PageSort page) {
        var re = super.list(entity, page);
        orgService.deptSet(re.getRows(), (p, p1) -> p1.setDeptName(p.getDeptName()));
        return re;
    }


    /**
     * 获取新闻栏目详细信息
     */
    @ApiOperation("详情")
    //@PreAuthorize("@ss.hasPermi('partybuilding:NewsSite:query')")
    @GetMapping(value = "/{id}")
    public R<NewsSite> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }

    /**
     * 新增新闻栏目
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('partybuilding:NewsSite:add')")
    @Log(title = "新闻站点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Long> add(@RequestBody NewsSite entity)
    {
        if(entity.getDeptId()==null||entity.getStatus()==null)
            return R.fail("参数错误");
        entity.setCreateBy(getUsername());
        entity.setCreateTime(new Date());
        var isAny=equeryable().where(p->p.eq(NewsSite::getDeptId,entity.getDeptId())).any();
        if (isAny)
        {
            return R.fail("部门站点已存在");
        }
        return super.add(entity);
    }

    /**
     * 修改新闻栏目
     */
    @ApiOperation("编辑")
    @PreAuthorize("@ss.hasPermi('partybuilding:NewsSite:edit')")
    @Log(title = "新闻站点", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<Long> edit(@RequestBody NewsSite entity)
    {
        entity.setUpdateBy(getUsername());
        entity.setUpdateTime(new Date());
        return super.edit(entity);
    }

    @ApiOperation("编辑,0关闭，1开启")
    @PreAuthorize("@ss.hasPermi('partybuilding:NewsSite:edit')")
    @Log(title = "新闻站点", businessType = BusinessType.UPDATE)
    @PostMapping("/editStatus")
    public R<Long> editStatus(@RequestBody NewsSite entity)
    {
        var re=equeryable().whereById(entity.getId()).firstNotNull();
        re.setUpdateBy(getUsername());
        re.setUpdateTime(new Date());
        re.setStatus(entity.getStatus());
        return super.edit(entity);
    }
    /**
     * 删除新闻栏目
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('partybuilding:NewsSite:remove')")
    @Log(title = "新闻站点", businessType = BusinessType.DELETE)
    @PostMapping("remove/{ids}")
    public R<Long> remove(@PathVariable Long[] ids)
    {
        return super.remove(ids);
    }
}
